<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>DOM | 掏钱的机器猫</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/vue-admin-ui/assets/logo.png">
    <meta name="description" content="掏钱的机器猫的个人博客,记录随笔与学习笔记，个人项目等">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/vue-admin-ui/assets/css/0.styles.fb55c927.css" as="style"><link rel="preload" href="/vue-admin-ui/assets/js/app.21d1badc.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/3.d02ddc64.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/1.38c8ef5a.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/14.e18000c4.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/8.79ebffb7.js" as="script"><link rel="prefetch" href="/vue-admin-ui/assets/js/10.d9a14dc0.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/11.284df538.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/12.530bdb86.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/13.3dde5c1a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/15.65b98979.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/16.953b2589.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/17.d6be13fa.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/18.a10ec5a3.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/19.f2ef083a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/20.9e024a1a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/21.b818c6cd.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/22.f3716a62.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/23.42bfb2a3.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/24.0382e6dc.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/25.9b1cf793.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/26.626abbca.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/27.d6b1a7e8.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/28.7381bce0.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/29.65b69672.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/30.70062600.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/31.036bec0e.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/32.1ec26c99.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/33.69485516.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/34.30a7370c.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/35.1e72f74a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/36.a6aaafe3.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/4.984f5e20.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/5.2cbf0f84.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/6.eab8daa0.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/7.41bb2a3c.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/9.e9ae19e3.js">
    <link rel="stylesheet" href="/vue-admin-ui/assets/css/0.styles.fb55c927.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-2d5f533b><div data-v-2d5f533b><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-2d5f533b data-v-2d5f533b><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-2d5f533b data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>掏钱的机器猫</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Ethan</span>
              
            <span data-v-64685f0e>2018 - </span>
            2022
          </a></span></div></div> <div class="hide" data-v-2d5f533b><header class="navbar" data-v-2d5f533b><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vue-admin-ui/" class="home-link router-link-active"><img src="/vue-admin-ui/assets/logo.png" alt="掏钱的机器猫" class="logo"> <span class="site-name">掏钱的机器猫</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
        插件文档
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/vue-admin-ui-lib/" class="nav-link"><i class="iconfont undefined"></i>
    vue-admin-ui-lib
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/submit-valid/" class="nav-link"><i class="iconfont undefined"></i>
    submit-valid
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/open-utils/" class="nav-link"><i class="iconfont undefined"></i>
    open-utils
  </a></li></ul></div></div><div class="nav-item"><a href="/vue-admin-ui/handle/handle/method.html" class="nav-link"><i class="iconfont undefined"></i>
    手写代码
  </a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
        个人站点
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/ethan6" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
    Gitee
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/Ethan66" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <a href="https://github.com/Ethan66/vue-admin-ui" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
      Github
      <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask" data-v-2d5f533b></div> <aside class="sidebar" data-v-2d5f533b><div class="personal-info-wrapper" data-v-ca798c94 data-v-2d5f533b><img src="/vue-admin-ui/assets/logo.png" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    Ethan
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>24</h3> <h6 data-v-ca798c94>文章</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>11</h3> <h6 data-v-ca798c94>标签</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
        插件文档
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/vue-admin-ui-lib/" class="nav-link"><i class="iconfont undefined"></i>
    vue-admin-ui-lib
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/submit-valid/" class="nav-link"><i class="iconfont undefined"></i>
    submit-valid
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/open-utils/" class="nav-link"><i class="iconfont undefined"></i>
    open-utils
  </a></li></ul></div></div><div class="nav-item"><a href="/vue-admin-ui/handle/handle/method.html" class="nav-link"><i class="iconfont undefined"></i>
    手写代码
  </a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
        个人站点
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/ethan6" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
    Gitee
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/Ethan66" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <a href="https://github.com/Ethan66/vue-admin-ui" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
      Github
      <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span></span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-admin-ui/article/browser/browser.html" class="sidebar-link">浏览器渲染机制</a></li><li><a href="/vue-admin-ui/article/browser/CSRF,XSS.html" class="sidebar-link">js之安全问题：Cookie、CSRF和XSS</a></li><li><a href="/vue-admin-ui/article/browser/css.html" class="sidebar-link">css</a></li><li><a href="/vue-admin-ui/article/browser/Dom.html" aria-current="page" class="active sidebar-link">DOM</a></li><li><a href="/vue-admin-ui/article/browser/html5.html" class="sidebar-link">html5</a></li><li><a href="/vue-admin-ui/article/browser/performanceOptimize.html" class="sidebar-link">js之页面性能优化</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>DOM</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Ethan</span>
              
            <span data-v-64685f0e>2018 - </span>
            2022
          </a></span></div></div> <div data-v-2d5f533b><main class="page"><div class="page-title" style="display:none;"><h1 class="title">DOM</h1> <div data-v-3b7f5bdf><i class="iconfont reco-account" data-v-3b7f5bdf><span data-v-3b7f5bdf>Ethan</span></i> <i class="iconfont reco-date" data-v-3b7f5bdf><span data-v-3b7f5bdf>2020-10-23</span></i> <!----> <i class="iconfont reco-tag tags" data-v-3b7f5bdf><span class="tag-item" data-v-3b7f5bdf>DOM</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p><img src="https://drscdn.500px.org/photo/298732607/q%3D80_m%3D2000/v2?webp=true&amp;sig=6ead84481c57db3d6028123e72ae6ac9edc27a4de554179bcfe09057fe54dcf9" alt=""></p> <h1 id="dom"><a href="#dom" class="header-anchor">#</a> DOM</h1> <h2 id="dom-2"><a href="#dom-2" class="header-anchor">#</a> DOM</h2> <p>1、Document Object Model: 文档对象模型。DOM的目的就是为了能让js操作html元素而制定的一个规范。
2、DOM就是由节点组成的。包括：元素节点。文本节点：标签中的文字（比如标签之间的空格、换行）。属性节点：标签的属性。顶级是html节点
3、HTML加载完毕，渲染引擎会在内存中把HTML文档，生成一个DOM树，getElementById是获取内中DOM上的元素节点。然后操作的时候修改的是该元素的属性。</p> <h3 id="获取元素"><a href="#获取元素" class="header-anchor">#</a> 获取元素</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>获取body、html和一般元素</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    // 获取body和html节点
    document.body
    document.documentElement
<div class="language- extra-class"><pre><code>// 获取元素
document.querySelector(&quot;body&quot;)
document.querySelector('div') // 假如有2个div取第一个
document.querySelectorAll(&quot;div&quot;)[0]
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="获取父子元素"><a href="#获取父子元素" class="header-anchor">#</a> 获取父子元素</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>获取父子，兄弟元素</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    // 获取父元素
    document.querySelector('body').parentNode
    document.querySelector('body').parentElement
<div class="language- extra-class"><pre><code>// 获取子节点
let el = document.querySelector('body')
el.firstElementChild // 获取第一个子元素
el.lastElementChild // 获取最后一个子元素
el.firstChild // 获取第一个子节点，不实用
el.lastChild // 获取最后一个子节点，不实用

// 获取兄弟节点
let el = document.querySelector('div')
el.nextElementSibling // 获取下一个元素节点
el.previousElementSibling // 获取上一个元素节点
el.nextSibling // 获取下一个节点，不实用，因为换行也是一个节点
el.previousSibling // 获取上一个节点，不实用，因为换行也是一个节点

// 获取所有兄弟元素
let el = document.querySelector('div')
el.parentNode.children // 获取所有子元素
el.parentNode.childNodes // 获取所有子节点
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="增删元素"><a href="#增删元素" class="header-anchor">#</a> 增删元素</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>增加、删除元素</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    // 增加元素
    let $el = document.createElement(&quot;div&quot;)
    $el.innerHTML = &quot;nihao&quot;
    document.querySelector(&quot;body&quot;).appendChild($el)
<div class="language- extra-class"><pre><code>// 删除元素
let $div = document.querySelector(&quot;div&quot;)
document.querySelector(&quot;body&quot;).removeChild($div)
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="其他操作"><a href="#其他操作" class="header-anchor">#</a> 其他操作</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>复制，替换，插入元素</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    // 复制节点
    let $div = document.querySelector('body')
    $div.cloneNode() // 只复制节点本身
    $div.cloneNode(true) // 复制节点和所有子节点
<div class="language- extra-class"><pre><code>// 替换元素
replaceChild(new, old)
// 插入元素
insertBefore(new, old)
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="获取class"><a href="#获取class" class="header-anchor">#</a> 获取class</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>获取元素的单个class、所有class</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    let el = document.querySelector('body')
    // 获取所有class
    el.className // 返回的是字符串
    el.classList  //返回的是数组
<div class="language- extra-class"><pre><code>// 获取单个class
el.classList.item(0) //class2
el.classList.item(1) // class1
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="增删class"><a href="#增删class" class="header-anchor">#</a> 增删class</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>增加、删除、替换class</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    // 添加class
    el.classList.add('class2', 'class1', 'class3')
    // 删除class
    el.classList.remove('class3')
    // 替换class
    el.classList.replace('class2', 'class6') // true, 最后为'class6 class1 class3'
    // 替换class
    el.classList.toggle('class1') // false, 因为有class1，所以删除，最后为‘class6 class3’
    el.classList.toggle('classs4') // true, 因为没有class4,所以添加，最后为'class6 class3 class4'
  </highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="判断class"><a href="#判断class" class="header-anchor">#</a> 判断class</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>增判断class存在</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    el.classList.contains(&quot;className&quot;)
  </highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="获取style"><a href="#获取style" class="header-anchor">#</a> 获取style</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>获取style，所有样式</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    let el = document.querySelector('body')
    // 获取style
    el.getAttribute('style') // 获取绑定在style属性上的全部值
    el.style.color // 获取绑定在style属性上的值，不能取style属性，不然会获取很多内容
<div class="language- extra-class"><pre><code>// 获取所有属性
window.getComputedStyle(el) // 包括默认样式和css中定义的样式，有兼容性，IE用currentStyle
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="删改style"><a href="#删改style" class="header-anchor">#</a> 删改style</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>修改、删除style</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    // 修改style
    el.style.fontSize = '18px'
    el.style.cssText = 'color: red; font-size: 20px;'
    el.setAttribute('style', 'color: red; font-size: 16px;')
    // 删除style
    el.removeAttribute('style')
  </highlight-code></div> <div class="code--button">显示代码</div></div> <h3 id="判断style属性"><a href="#判断style属性" class="header-anchor">#</a> 判断style属性</h3> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>判断style属性是否存在</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    el.hasAttribute('style')
  </highlight-code></div> <div class="code--button">显示代码</div></div> <h2 id="事件"><a href="#事件" class="header-anchor">#</a> 事件</h2> <h3 id="四种事件模型的区别"><a href="#四种事件模型的区别" class="header-anchor">#</a> 四种事件模型的区别</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>// DOM0事件模型：直接给元素绑定事件属性
&lt;div onclick=&quot;fn()&quot;&gt;btn&lt;/div&gt;
element.onclick = function () {} // 执行的函数以最后一个绑定的函数为准

// 删除事件模型
element.onclick = null

// DOM2事件模型
// IE通过attachEvent绑定：有2个过程：事件处理阶段和事件冒泡阶段，先执行目标元素绑定的事件，再从目标元素冒泡到document，最后到window

通过addEventListener绑定，有3个过程：事件捕获阶段，事件处理阶段和事件冒泡阶段。先从window到document一直向下传播到目标元素，依次检查经过的节点是否绑定了事件监听函数，如果有则执行。后面两个阶段和 IE 事件模型的两个阶段相同
 element.addEventListener('click', function () {}, false);

// 1、true表示捕获阶段触发，false表示冒泡阶段触发(默认)
// 2、一般的浏览器：div -&gt; body -&gt; html -&gt; document -&gt; window
// 3、绑定了2个函数，都会执行
// 4、不能冒泡的事件: blur、focus、load、unload、onmouseenter、onmouseleave

 // DOM3事件模型：增加很多事件类型，如焦点事件，鼠标事件，键盘事件
 element.addEventListener('keyup', function () {}, false);
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="事件流"><a href="#事件流" class="header-anchor">#</a> 事件流</h3> <ul><li>捕获阶段–事件从 window 开始，然后向下到每个元素，直到到达目标元素事件或event.target。</li> <li>目标阶段–事件已达到目标元素。</li> <li>冒泡阶段–事件从目标元素冒泡，然后上升到每个元素，直到到达 window。</li></ul> <h3 id="常见事件"><a href="#常见事件" class="header-anchor">#</a> 常见事件</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>// 点击事件：
onclick, ondblclick // 双击

// 键盘事件：
onkeyup // 释放键盘时, onkeydown // 按下键盘时

// 焦点事件：
oninput // 文本框输入时, onchange // 文本内容改变时, onfocus // 文本框聚焦时, onblur // 失焦时

// 鼠标事件
onmouseover // 鼠标移进事件源时, onmousemove // 鼠标在事件源移动时, onmouseout // 鼠标移出时
onload // 网页文档或文本或图片加载
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="事件对象"><a href="#事件对象" class="header-anchor">#</a> 事件对象</h3> <p>1、触发DOM上的某个事件时，都会产生一个事件对象event。
2、兼容性：event = event || window.event(IE6,7,8)</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>// event常用属性
event.bubbles // true表示事件可以冒泡，不是所有的事件都可以冒泡
event.stopPropagation() // 阻止冒泡，当bubbles为true可以使用
event.cancelable // 为true表示可以取消事件的默认事件
event.preventDefault() // 取消默认事件，当cancelable为true时
event.currentTarget // 执行事件时的那个元素，即currentTarget始终 === this
event.targget // 真正触发的事件的元素,比如事件点击的元素
event.type // 被触发的事件的类型,如'click','input'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="样式"><a href="#样式" class="header-anchor">#</a> 样式</h2> <h3 id="记忆方法"><a href="#记忆方法" class="header-anchor">#</a> 记忆方法</h3> <ul><li>clientWidth/clientHeight: 获取content+padding</li> <li>clientTop/clientLeft: 获取border</li> <li>offsetWidth/offsetHeight: 获取content+padding+border</li> <li>offsetTop/offsetLeft: 元素没有设置left, top时获取的margin</li> <li>getComputedStyle(el).margin: 获取元素的margin（带px）</li> <li>getComputedStyle(el).marginTop: 获取元素的marginTop（带px）</li></ul> <h3 id="获取元素的各种宽高度"><a href="#获取元素的各种宽高度" class="header-anchor">#</a> 获取元素的各种宽高度</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>let el = document.querySelector('.cls1')
// 元素的所有宽高（content、padding、border）
el.offsetWidth
el.offsetHeight

// 元素的宽高（content和padding）
el.clientWidth
el.clientHeight

// 元素的border值
el.clientTop // 元素的上边框宽度
el.clientLeft // 元素的左边框宽度

// 元素的margin值
getComputedStyle(el).marginTop // 注意：展示的是数值带px
getComputedStyle(el).margin // 注意：展示的是数值带px，比如：'5px 6px 7px 8px'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="元素的偏移量"><a href="#元素的偏移量" class="header-anchor">#</a> 元素的偏移量</h3> <p>offsetTop/offsetLeft：获取元素距离当前最近非position: static;父元素的上偏移/左偏移，假如父元素都是position: static;则指向body元素。假如元素有设定left,top,则按照此值来</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>el.offsetTop
el.offsetLeft
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="鼠标的偏移量"><a href="#鼠标的偏移量" class="header-anchor">#</a> 鼠标的偏移量</h3> <ul><li>clientX：当鼠标事件发生时，鼠标相对于浏览器（这里说的是浏览器的有效区域）x轴的位置；</li> <li>clientY：当鼠标事件发生时，鼠标相对于浏览器（这里说的是浏览器的有效区域）y轴的位置；</li> <li>screenX：当鼠标事件发生时，鼠标相对于显示器屏幕x轴的位置；// 不常用</li> <li>screenY：当鼠标事件发生时，鼠标相对于显示器屏幕y轴的位置；// 不常用</li> <li>offsetX：当鼠标事件发生时，鼠标相对于事件源x轴的位置</li> <li>offsetY：当鼠标事件发生时，鼠标相对于事件源y轴的位置</li></ul> <h3 id="元素相对于浏览器的偏移量"><a href="#元素相对于浏览器的偏移量" class="header-anchor">#</a> 元素相对于浏览器的偏移量</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>let location = document.querySelector('#id').getBoundingClientRect() // 相对于去除菜单栏的窗口
location.top // 元素顶部相对于窗口的顶部距离
location.left // 元素左边相对于窗口的左边距离
location.bottom // 元素底部相对于窗口的顶部距离
location.right // 元素右边相对于窗口的左边距离
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="元素的滚动"><a href="#元素的滚动" class="header-anchor">#</a> 元素的滚动</h3> <ul><li>scrollWidth/scrollHeight：获取元素的文档宽高，包含溢出部分，假如内容没有溢出，那就展示本身元素的宽高（padding+content）</li> <li>scrollTop/scrollLeft: 获取元素滚动的举例</li></ul> <h3 id="页面屏幕宽高兼容性"><a href="#页面屏幕宽高兼容性" class="header-anchor">#</a> 页面屏幕宽高兼容性</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="可视区域的宽高"><a href="#可视区域的宽高" class="header-anchor">#</a> 可视区域的宽高</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>document.documentElement.clientWidth
document.documentElement.clientHeight
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="浏览器的宽高"><a href="#浏览器的宽高" class="header-anchor">#</a> 浏览器的宽高</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>window.innerWidth //除去菜单栏的窗口宽度
window.innerHeight//除去菜单栏的窗口高度
window.outerWidth//包括菜单栏的窗口宽度
window.outerHeight//包括菜单栏的窗口宽度
window.screenTop//距离屏幕的高度
window.screenLeft//距离屏幕的宽度
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="电脑屏幕的宽高"><a href="#电脑屏幕的宽高" class="header-anchor">#</a> 电脑屏幕的宽高</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>window.screen.height//电脑屏幕的高度
window.screen.width//电脑屏幕的宽度
window.screen.availHeight//电脑屏幕的可利用高度
window.screen.availWidth//电脑屏幕的可利用宽度
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">2022/7/8 上午10:51:20</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/vue-admin-ui/assets/js/app.21d1badc.js" defer></script><script src="/vue-admin-ui/assets/js/3.d02ddc64.js" defer></script><script src="/vue-admin-ui/assets/js/1.38c8ef5a.js" defer></script><script src="/vue-admin-ui/assets/js/14.e18000c4.js" defer></script><script src="/vue-admin-ui/assets/js/8.79ebffb7.js" defer></script>
  </body>
</html>
